import { getCurrentWindow } from '@tauri-apps/api/window';

export class WindowService {
  private static instance: WindowService;
  private window = getCurrentWindow();

  static getInstance(): WindowService {
    if (!WindowService.instance) {
      WindowService.instance = new WindowService();
    }
    return WindowService.instance;
  }

  // 设置窗口为全屏
  async setFullscreen(fullscreen: boolean = true): Promise<void> {
    try {
      await this.window.setFullscreen(fullscreen);
    } catch (error) {
      console.error('Failed to set fullscreen:', error);
    }
  }

  // 最大化窗口
  async maximize(): Promise<void> {
    try {
      await this.window.maximize();
    } catch (error) {
      console.error('Failed to maximize window:', error);
    }
  }

  // 还原窗口
  async unmaximize(): Promise<void> {
    try {
      await this.window.unmaximize();
    } catch (error) {
      console.error('Failed to unmaximize window:', error);
    }
  }

  // 最小化窗口
  async minimize(): Promise<void> {
    try {
      await this.window.minimize();
    } catch (error) {
      console.error('Failed to minimize window:', error);
    }
  }

  // 关闭窗口
  async close(): Promise<void> {
    try {
      await this.window.close();
    } catch (error) {
      console.error('Failed to close window:', error);
    }
  }

  // 设置窗口大小
  async setSize(width: number, height: number): Promise<void> {
    try {
      const { LogicalSize } = await import('@tauri-apps/api/window');
      await this.window.setSize(new LogicalSize(width, height));
    } catch (error) {
      console.error('Failed to set window size:', error);
    }
  }

  // 设置窗口位置
  async setPosition(x: number, y: number): Promise<void> {
    try {
      const { LogicalPosition } = await import('@tauri-apps/api/window');
      await this.window.setPosition(new LogicalPosition(x, y));
    } catch (error) {
      console.error('Failed to set window position:', error);
    }
  }

  // 居中窗口
  async center(): Promise<void> {
    try {
      await this.window.center();
    } catch (error) {
      console.error('Failed to center window:', error);
    }
  }

  // 设置窗口可调整大小
  async setResizable(resizable: boolean): Promise<void> {
    try {
      await this.window.setResizable(resizable);
    } catch (error) {
      console.error('Failed to set window resizable:', error);
    }
  }

  // 设置窗口最小尺寸
  async setMinSize(width: number, height: number): Promise<void> {
    try {
      const { LogicalSize } = await import('@tauri-apps/api/window');
      await this.window.setMinSize(new LogicalSize(width, height));
    } catch (error) {
      console.error('Failed to set window min size:', error);
    }
  }

  // 设置窗口最大尺寸
  async setMaxSize(width: number, height: number): Promise<void> {
    try {
      const { LogicalSize } = await import('@tauri-apps/api/window');
      await this.window.setMaxSize(new LogicalSize(width, height));
    } catch (error) {
      console.error('Failed to set window max size:', error);
    }
  }

  // 检查窗口是否最大化
  async isMaximized(): Promise<boolean> {
    try {
      return await this.window.isMaximized();
    } catch (error) {
      console.error('Failed to check if window is maximized:', error);
      return false;
    }
  }

  // 检查窗口是否全屏
  async isFullscreen(): Promise<boolean> {
    try {
      return await this.window.isFullscreen();
    } catch (error) {
      console.error('Failed to check if window is fullscreen:', error);
      return false;
    }
  }
}

export const windowService = WindowService.getInstance();
